package com.sgb.kjwl.view.ui.activity.self.offer;

import android.content.Context;
import android.content.Intent;
import android.util.TypedValue;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;
import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityTruckApplyBinding;
import com.sgb.kjwl.di.transport.DaggerTransportComponent;
import com.sgb.kjwl.di.transport.TransportModule;
import com.sgb.kjwl.model.entity.self.TruckApplyEntity;
import com.sgb.kjwl.view.ui.activity.self.logistics.OfferOwnerDetailsActivity;
import com.sgb.kjwl.view.ui.adapter.self.offer.TruckApplyAdapter;
import com.sgb.kjwl.viewmodel.self.offer.TruckApplyModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;

import java.util.List;

/**
 * Describe:
 * Author:  yangLei
 * Date:    2021/4/7
 */
public class TruckApplyActivity extends AppActivity {
    private ActivityTruckApplyBinding binding;
    TruckApplyModel viewModel;
    TruckApplyAdapter adapter;

    public static void start(Context context) {
        Intent intent = new Intent(context, TruckApplyActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        context.startActivity(intent);
    }

    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_truck_apply);
        binding.topBar.titleView.setText("货车报名");
        binding.topBar.titleView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 17);
        binding.topBar.ivBack.setOnClickListener(v -> finish());
        viewModel = new ViewModelProvider(this).get(TruckApplyModel.class);
        if (adapter == null)
            adapter = new TruckApplyAdapter(getActivity(), null);
        DividerItemDecoration divider = new DividerItemDecoration(this, DividerItemDecoration.VERTICAL);
        divider.setDrawable(ContextCompat.getDrawable(this, R.drawable.shape_list_divide));
        binding.recyclerView.addItemDecoration(divider);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        binding.recyclerView.setLayoutManager(layoutManager);
        binding.recyclerView.setAdapter(adapter);
        binding.recyclerView.setLoadingMoreEnabled(false);
        binding.recyclerView.setPullRefreshEnabled(false);
        binding.smartRefresh.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                viewModel.getTrucks(TruckApplyActivity.this, false,true);
                binding.smartRefresh.finishLoadMore();
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                viewModel.getTrucks(TruckApplyActivity.this, true,true);
                binding.smartRefresh.finishRefresh();
            }
        });

        adapter.setOnItemClick(new AdapterOnItemClick<TruckApplyEntity>() {
            @Override
            public void onItemClick(TruckApplyEntity truckApplyEntity, int position) {
                // 跳转报价车主的报价详情
                OfferOwnerDetailsActivity.start(getActivity(), truckApplyEntity.getGoodsOfferNo(), false);
            }
        });

        viewModel.refreshLiveData.observe(this, truckApplyEntities -> showData(truckApplyEntities));
        viewModel.loadMoreLiveData.observe(this, truckApplyEntities -> loadMoreData(truckApplyEntities));
    }

    @Override
    protected void onResume() {
        super.onResume();
        viewModel.getTrucks(TruckApplyActivity.this, true,true);
    }

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerTransportComponent.builder()
                .appComponent(appComponent)
                .transportModule(new TransportModule(this))
                .build().inject(this);
    }

    /**
     * 展示第一页数据
     *
     * @param list
     */
    public void showData(List<TruckApplyEntity> list) {
        if (list == null)
            return;
        if (list.size() == 0) {
            binding.emptyView.setVisibility(View.VISIBLE);
        } else {
            binding.emptyView.setVisibility(View.GONE);
            adapter.setDatas(list);
        }
    }

    /**
     * 加载更多数据
     *
     * @param list
     */
    public void loadMoreData(List<TruckApplyEntity> list) {
        if (list == null)
            return;
        if (list.size() > 0) {
            adapter.addDatas(list);
        }
    }
}
